import qqmap from '../../../utils/qqmap'
// map.js
Page({
  data: {
    // 地图中心点
    latitude: 27.683346,
    longitude: 113.848006,
    // 标几点
    markers: [
      // 终点
      {
        id: 1,
        latitude: 28.146841,
        longitude: 113.065296,
        width: 24,
        height: 30,
      },
      // 起点
      {
        id: 2,
        latitude: 28.622462,
        longitude: 115.792786,
        width: 40,
        height: 40,
        iconPath: '/static/images/marker.png',
      },
    ],
  },
  onLoad({
    id
  }) {
    this.getrepairDetail(id)
    this.getPolyline()
  },

  // 调用位置服务 路线规划
  getPolyline() {
    qqmap.direction({
      mode: 'bicycling',
      // 起点
      from: '28.622462,115.792786',
      // 终点
      to: '28.146841,113.065296',
      success: ({
        result
      }) => {
        const coors = result.routes[0].polyline
        const points = []
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000
        }

        for (let i = 0; i < coors.length; i += 2) {
          points.push({
            latitude: coors[i],
            longitude: coors[i + 1]
          })
        }
        this.setData({
          latitude: points[300].latitude,
          longitude: points[300].longitude,
          polyline: [{
            points,
            color: '#5591af',
            width: 4
          }, ],
        })
      },
    })
  },

  // 获取保修详情
  async getrepairDetail(id) {
    if (!id) return
    const {
      code,
      message,
      data: repairDetail
    } = await wx.http.get(`/repair/${id}`)
    if (code !== 10000) return wx.utils.toast(message)
    this.setData({
      repairDetail,
    })
  },

  // 取消保修
  async cancel(e) {
    const {
      code,
      message
    } = await wx.http.put('/cancel/repaire/' + e.mark.id)
    if (code !== 10000) return wx.utils.toast()
    wx.utils.toast(message)
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  },
})